package com.android.tea;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;

import com.android.tea.api.ApiResult;
import com.android.tea.api.ApiReturnResultListener;
import com.android.tea.util.TeaLog;
import com.android.tea.util.ToastManager;
import com.android.tea.widght.SettingItemButton;
import com.android.tea.widght.SettingItemPanel;

public class SettingActivity extends Activity implements OnClickListener {
	private static final int ID_USER_ITEM = 2012;
	private static final int ID_CHAT = 2013;
	private static final String TAG = "SettingActivity";
	private static final int ID_ITEM1 = 2014;
	private static final int ID_ITEM2 = 2015;
	private static final int ID_ITEM3 = 2016;
	private static final int ID_ITEM4 = 2017;
	private InputMethodManager inputMethodManager;
	private LinearLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		layout = (LinearLayout) findViewById(R.id.setting_panels);
		initPanels();
	}

	private void initPanels() {
		init1();
		// init2();
		// init3();
		init4();
		init5();
	}

	ApiReturnResultListener apiReturnResultListener = new ApiReturnResultListener() {

		@Override
		public <T> void onReturnSucceedResult(int requestCode,
				ApiResult<T> apiResult) {
			TeaLog.e("aaaa", "onReturnSucceedResult");
		}

		@Override
		public <T> void onReturnFailResult(int requestCode,
				ApiResult<T> apiResult) {
			TeaLog.e("aaaa", "onReturnFailResult");
		}
	};

	private void init1() {
		SettingItemPanel sip = new SettingItemPanel(this);
		SettingItemButton sib = new SettingItemButton(this);
		sib.setIdUserItem(ID_USER_ITEM);
		sib.setItemOnClickListener(this);
		sib.setTitle("用户中心");
		sip.addItem(sib);
		// SettingItemButton sib1 = new SettingItemButton(this);
		// sib1.setTitle("更改用户");
		// sip.addItem(sib1);
		// sib1.setItemOnClickListener(this);
		// SettingItemButton sib2 = new SettingItemButton(this);
		// sib2.setTitle("新消息提醒");
		// sib2.setItemOnClickListener(this);
		// sip.addItem(sib2);
		addView(sip);
	}

	public void init2() {
		SettingItemPanel chatPanel = new SettingItemPanel(this);
		SettingItemButton chat = new SettingItemButton(this);
		chat.setOnClickListener(this);
		chat.setIdUserItem(ID_CHAT);
		chat.setTitle("聊天背景");
		chatPanel.addItem(chat);
		addView(chatPanel);
	}

	public void init3() {
		SettingItemPanel sip = new SettingItemPanel(this);
		SettingItemButton sib = new SettingItemButton(this);
		sib.setTitle("绑定帐号");
		sip.addItem(sib);
		SettingItemButton sib1 = new SettingItemButton(this);
		sib1.setTitle("同步微博");
		sib1.setMessage("未同步");
		sip.addItem(sib1);
		SettingItemButton sib2 = new SettingItemButton(this);
		sib2.setTitle("推荐好友");
		sip.addItem(sib2);
		addView(sip);
	}

	public void init4() {
		SettingItemPanel sip = new SettingItemPanel(this);
		/*
		 * SettingItemButton sib = new SettingItemButton(this);
		 * sib.setTitle("插件"); sib.setMessageColor(Color.WHITE);
		 * sib.setMessageBackground(R.drawable.newbackground); sip.addItem(sib);
		 * SettingItemButton sib1 = new SettingItemButton(this);
		 * sib1.setTitle("黑名单"); sip.addItem(sib1); SettingItemButton sib2 = new
		 * SettingItemButton(this); sib2.setTitle("隐私"); sip.addItem(sib2);
		 * 
		 * SettingItemButton sib3 = new SettingItemButton(this);
		 * sib3.setTitle("字体大小"); sib3.setMessage("普通字体"); sip.addItem(sib3);
		 * 
		 * SettingItemButton sib4 = new SettingItemButton(this);
		 * sib4.setTitle("流量统计"); sip.addItem(sib4);
		 */

		SettingItemButton sib5 = new SettingItemButton(this);
		sib5.setTitle("清除缓存");
		sib5.setItemOnClickListener(this);
		sib5.setIdUserItem(ID_ITEM1);
		sip.addItem(sib5);
		addView(sip);
	}

	public void init5() {

		SettingItemPanel sip = new SettingItemPanel(this);
		SettingItemButton sib = new SettingItemButton(this);
		sib.setIdUserItem(ID_ITEM2);
		sib.setTitle("帮助与反馈");
		sib.setItemOnClickListener(this);
		sip.addItem(sib);
		SettingItemButton sib1 = new SettingItemButton(this);
		sib1.setIdUserItem(ID_ITEM3);
		sib1.setTitle("软件更新");
		sib1.setItemOnClickListener(this);
		sip.addItem(sib1);
		SettingItemButton sib2 = new SettingItemButton(this);
		sib2.setIdUserItem(ID_ITEM4);
		sib2.setTitle("关于茶百科");
		sib2.setItemOnClickListener(this);
		sip.addItem(sib2);

		addView(sip);
	}

	private void addView(View view) {
		if(layout!=null) {
			if (layout.getChildCount() > 0) {
				LinearLayout layout = new LinearLayout(getApplicationContext());
				
				LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, 15);
				layout.setLayoutParams(params);
				this.layout.addView(layout);
			}
			layout.addView(view);
		}
	}
	
	@Override
	public void onBackPressed() {
		Activity parent = this.getParent();
		if(parent!=null) {
			parent.onBackPressed();
		}else{
			super.onBackPressed();
		}
	}

	@Override
	public void onClick(View v) {
		Log.i(TAG, "==================" + ((RelativeLayout) v).getId());
		switch (v.getId()) {
		case ID_USER_ITEM:
			Log.i(TAG, "--------------------");
			Intent userIntent = new Intent(this, MemberCenterActivity.class);
			startActivity(userIntent);
			break;
		case ID_CHAT:
			TeaLog.e(TAG, "-----------ID_CHAT---------");
			break;
		case ID_ITEM1:
			Log.i(TAG, "--------------------");
			new AlertDialog.Builder(this).setTitle("清除缓存").setMessage("确认清除缓存吗？").setPositiveButton("确认", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			}).show();
			break;
		case ID_ITEM2:
			Log.i(TAG, "--------------------");
			Intent chatIntent = new Intent(this,FeedBackActivity.class);
			startActivity(chatIntent);
			break;
		case ID_ITEM3:
			Log.i(TAG, "--------------------");
//			Toast.makeText(this, R.string.Setting_Update_Version, Toast.LENGTH_SHORT).show();
			ToastManager.showToast(this, R.string.Setting_Update_Version);
			break;
		case ID_ITEM4:
			Log.i(TAG, "--------------------");
			Intent feedIntent = new Intent(this,AboutActivity.class);
			startActivity(feedIntent);
			break;

		default:
			break;
		}
	}

}
